<?php
namespace App\Libs\Goods;
use \App\Libs\DB\PailePdo;
use \App\Libs\Exception\RuntimeException;
use \App\Libs\Base\Constant;
use App\Models\Goods\Goods as GoodsModel;
class Goods extends \App\Libs\Base\AccessObject
{
	/**
	*	商品搜索 
	*	return 店铺信息及店铺下的商品信息
	*/
	public static function goodsStoreSearch( $params ){
		$where = ' where 1=1 ';
		$params = array_merge( [ 'page'=>1 , 'size'=>10 ] , $params );
		
		
		if( !empty( $params['key'] ) ){
			//查询是否是全词类型查找。
			$typeSql = " select * from t_paile_store_type where type_name = '{$params['key']}'";
			$typeData = PailePdo::find($typeSql);
			if(!empty($typeData)){
				//是全词查找，判断是否是一级全词
				if( $typeData['fid'] == 0 ){
					//一级全词
					$typeOneSql = " select * from t_paile_store_type where fid = {$typeData['id']}";
					$typeOneData = PailePdo::findAll( $typeOneSql );
					if(count($typeOneData) > 0 ){
						$typeNameIn = [];
						foreach( $typeOneData as $k=>$v ){
							$typeNameIn[] = $v['id'];
						}
						$where .= " and type.id in(".implode(',',$typeNameIn).")";
					}else{
						return false;
					}
				}else{
					//二级全词
					$where .= " and type.type_name = '{$params['key']}'";
				}
			}else{
				$where .= " and (store.store_name LIKE '%{$params['key']}%' or type.type_name LIKE '%{$params['key']}%')";
			}
		}
		
		$where .= " and store.city = '{$params['city']}' and store.isshow = 2 and type.isshow = 2";//城市
		
		//排序
		$filed = ",round(6378.138*2*asin(sqrt(pow(sin( ({$params['lat']}*pi()/180-latitude*pi()/180)/2),2)+cos({$params['lat']}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$params['lnt']}*pi()/180-longitude*pi()/180)/2),2)))*1000) distance";
		$order = " ";
		if( $params['order'] == 'distance' ){
			//正序
			$order .= '  order by distance asc ';
		}else if( $params['order'] == 'sale_num' ){
			//倒序
			$order .= '  order by store.store_sales desc  ';
		}else{
			$order .= '  order by distance asc ';
		}
		//类别
		if( !empty( $params['type'] ) ){
			$where .= " and store.store_type = '{$params['type']}' ";
		}
		//附近
		$having = '';
		/*if( !empty( $params['nearby'] ) ){
			$having = " having distance > {$params['nearby']} ";
		}*/
		
		$limit = "limit ".( ($params['page']-1)*$params['size'] ).','.$params['size'];
		
		$sql = "SELECT store.id,store.store_name,store.store_grade,store.store_img,store.store_sadd,store.longitude,store.latitude,type.type_name{$filed} FROM t_paile_store store LEFT JOIN t_paile_store_type type ON store.store_type = type.id ".$where.$having.$order.$limit;
		@$fp = fopen('./b.txt', 'a+');
	@fwrite($fp, PHP_EOL . 'sql：'. date('Y-m-d H:i:s',time()) . '##111##' . $sql  .PHP_EOL);
@fclose($fp);
		$data = PailePdo::findAll($sql);
		if(count($data) > 0 ){
			return $data;
		}else{
			return false;
		}
	}
	
	
	/**
	*	爆款商品
	*/
	public static function hotGoods( $params ){
		$where = ' where 1=1 ';
		$params = array_merge( [ 'page'=>1 , 'size'=>10 ] , $params );
		$filed = '';
		if( !empty( $params['lat'] ) && !empty( $params['lnt'] ) ){
			//排序
			$filed = ",round(6378.138*2*asin(sqrt(pow(sin( ({$params['lat']}*pi()/180-latitude*pi()/180)/2),2)+cos({$params['lat']}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$params['lnt']}*pi()/180-longitude*pi()/180)/2),2)))*1000) distance";
		}
		
		$where .= " and store.city = '{$params['city']}' ";//城市
		
		$limit = "limit ".( ($params['page']-1)*$params['size'] ).','.$params['size'];
		$sql = "SELECT hot.gid,hot.sid,hot.ctime,goods.goods_name,goods.goods_price,goods.sale_price,goods.goods_img,store.store_sadd{$filed} FROM t_paile_hot hot LEFT JOIN t_paile_goods goods ON hot.gid = goods.id LEFT JOIN t_paile_store store ON hot.sid = store.id".$where.$limit;
		$data = PailePdo::findAll($sql);
		//格式化价格;
        foreach($data as $key => $val){
            $priceArray['goods_price'] = GoodsModel::formatPrice($val['goods_price']);
            $priceArray['sale_price'] = GoodsModel::formatPrice($val['sale_price']);
            $data[$key] = array_merge($val,$priceArray);
        }
		if(count($data) > 0 ){
			return $data;
		}else{
			return false;
		}
	}
	
	/**
	*	根据sid获取商品
	*
	*/
	public static function getGoodsSidIn( $params ){
		$sql = "SELECT id,sid,goods_name,goods_price,sale_price,goods_sold,goods_sold goods_grade FROM t_paile_goods WHERE sid in ({$params}) and is_experience = 2 and show_status!=1";
		$data = PailePdo::findAll($sql);
        foreach($data as $key => $val){
            $priceArray['goods_price'] = GoodsModel::formatPrice($val['goods_price']);
            $priceArray['sale_price'] = GoodsModel::formatPrice($val['sale_price']);
            $data[$key] = array_merge($val,$priceArray);
        }
		if(count($data) > 0 ){
			return $data;
		}else{
			return false;
		}
	}
}
